﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using r2mp_DataStructures;

namespace r2mp_GUI
{

    /// <summary>
    /// This class has been replaced by r2mp_MTA.MTAWorker
    /// </summary>
    public class MTAWorker_Deprecated
    {
        MTA mta;
        int numberOfColumns;
        int numberOfRows;
        int globalNumberOfColumns;
        int aktYear;
        int m;
        int mDraw;

        public MTAWorker_Deprecated(MTA mta)
        {
            this.mta = mta;
            initWorker();
        }

        public void initWorker()
        {
            int mDraw = 0;
            int lowestYear = mta.Milestones[0].PlannedCompletionDate.Year;
            int highestYear = 0;
            int lowestMonth = mta.Milestones[0].PlannedCompletionDate.Month;
            int highestMonth = 0;

            foreach (Milestone ms in mta.Milestones)
            {
                if (ms.PlannedCompletionDate.Year < lowestYear)
                    lowestYear = ms.PlannedCompletionDate.Year;
                if (ms.PlannedCompletionDate.Year > highestYear)
                    highestYear = ms.PlannedCompletionDate.Year;
                if (ms.PlannedCompletionDate.Month < lowestMonth)
                    lowestMonth = ms.PlannedCompletionDate.Month;
                if (ms.PlannedCompletionDate.Month + ms.TimeshiftSum > highestMonth)
                    highestMonth = ms.PlannedCompletionDate.Month + ms.TimeshiftSum;
            }

            int jahr = 12;
            while (jahr < highestMonth)
            {
                jahr += 6;
            }
            numberOfColumns = jahr;
            numberOfRows = jahr;
            globalNumberOfColumns = numberOfColumns;
            aktYear = lowestYear - 1;
        }

        public void drawMTA(Graphics graphics)
        {
            //Variablendeklarationen
            int actColumn = 0;
            int actRow = 0;
            int abstandLinks = 40;
            int abstandOben = 40;
            int x = abstandLinks;
            int y = abstandOben;
            int width = 40;
            int heigth = 40;
            int z = numberOfRows - 1; //Variable zur Hilfe für Beschriftung auf Y-Achse
            Font f = new Font("Arial", 6);

            //Zeichnung des Rasters mit Beschriftung
            while (actRow < numberOfRows)
            {
                while (actColumn < numberOfColumns)
                {
                    if (actRow == 0) //Erste Zeile wird gesondert behandelt, wegen Beschriftung auf X-Achse
                    {
                        if (m % 12 == 0)
                        {
                            aktYear++;
                            m = 0;
                        }
                        string month = getMonthname(m, aktYear);
                        m++;
                        graphics.DrawString(month, f, Brushes.Black, abstandLinks + abstandLinks / 3 - 4 + mDraw * width, 0);
                        mDraw++;
                    }
                    if (actColumn == 0)
                    {
                        if (actRow != 0)
                        {
                            string month = getMonthname(actRow, aktYear);
                            graphics.DrawString(month, f, Brushes.Black, 0, y + heigth / 3);
                            if (actRow % 12 == 0)
                                aktYear += 1;
                        }
                        else
                        {
                            aktYear = 2011;
                            string month = getMonthname(actRow, aktYear);
                            graphics.DrawString(month, f, Brushes.Black, 0, y + heigth / 3);
                        }
                    }
                    graphics.DrawRectangle(Pens.Black, x, y, width, heigth);
                    x += width;
                    actColumn++;
                }
                y += heigth;
                numberOfColumns--;
                x = abstandLinks;
                actColumn = 0;
                z--;
                actRow++;
                m = 0;
            }
            graphics.DrawLine(Pens.Black, new Point(abstandLinks, abstandOben + globalNumberOfColumns * heigth), new Point(abstandLinks + numberOfRows * width, abstandOben));
            graphics.DrawLine(Pens.Black, new Point(abstandLinks + numberOfRows * width, abstandOben), new Point(abstandLinks + numberOfRows * width, abstandOben + globalNumberOfColumns * heigth));
            graphics.DrawLine(Pens.Black, new Point(abstandLinks, abstandOben + globalNumberOfColumns * heigth), new Point(abstandLinks + numberOfRows * width, abstandOben + globalNumberOfColumns * heigth));
            graphics.FillPolygon(Brushes.LightGray, new Point[3] { new Point(abstandLinks, abstandOben + globalNumberOfColumns * heigth), new Point(abstandLinks + numberOfRows * width, abstandOben), new Point(abstandLinks + numberOfRows * width, abstandOben + globalNumberOfColumns * heigth) });

            // Farbauswahl und graphische Darstellung der Meilensteine
            int spalte = 0;
            int a = 0;
            Pen p = new Pen(Color.Blue, 5);
            Pen q = new Pen(Color.Blue, 2);
            Point startPoint;
            Point endPoint;
            foreach (Milestone ms in mta.Milestones)
            {
                switch (ms.PlannedCompletionDate.Month)
                {
                    case 5:
                        p = new Pen(Color.Yellow, 5);
                        q = new Pen(Color.Yellow, 2);
                        break;
                    case 7:
                        p = new Pen(Color.Green, 5);
                        q = new Pen(Color.Green, 2);
                        break;
                    case 11:
                        p = new Pen(Color.Red, 5);
                        q = new Pen(Color.Red, 2);
                        break;
                }
                int month = ms.PlannedCompletionDate.Month;
                startPoint = new Point(abstandLinks + spalte * width + width / 2, abstandOben + (numberOfRows - month + a) * heigth + heigth / 2);
                graphics.DrawEllipse(p, startPoint.X, startPoint.Y, 4, 4);
                foreach (int timeshift in ms.Timeshifts)
                {
                    a += timeshift * -1;
                    endPoint = new Point(abstandLinks + spalte * width + width + width / 2, abstandOben + (numberOfRows - month + a) * heigth + heigth / 2);
                    graphics.DrawEllipse(p, abstandLinks + spalte * width + width + width / 2, abstandOben + (numberOfRows - month + a) * heigth + heigth / 2, 4, 4);
                    graphics.DrawLine(q, startPoint, endPoint);
                    spalte++;
                    startPoint = endPoint;
                }
                spalte = 0;
                a = 0;
            }
        }

        // Funktion zur Ermittlung des Monats
        public string getMonthname(int number, int yearFull)
        {
            if (number > 11)
                number -= 12;
            string s = yearFull.ToString();
            int year = int.Parse(s.Substring(2, 2));
            string monthname = String.Empty;
            switch (number)
            {
                case 0:
                    monthname = "Jan" + year.ToString();
                    break;
                case 1:
                    monthname = "Feb" + year.ToString();
                    break;
                case 2:
                    monthname = "Mar" + year.ToString();
                    break;
                case 3:
                    monthname = "Apr" + year.ToString();
                    break;
                case 4:
                    monthname = "Mai" + year.ToString();
                    break;
                case 5:
                    monthname = "Jun" + year.ToString();
                    break;
                case 6:
                    monthname = "Jul" + year.ToString();
                    break;
                case 7:
                    monthname = "Aug" + year.ToString();
                    break;
                case 8:
                    monthname = "Sep" + year.ToString();
                    break;
                case 9:
                    monthname = "Okt" + year.ToString();
                    break;
                case 10:
                    monthname = "Nov" + year.ToString();
                    break;
                case 11:
                    monthname = "Dez" + year.ToString();
                    year++;
                    break;
            }
            return monthname;
        }
    }
}
